﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace QuanLyBepAnTuThien
{
    public partial class UcMTQ : UserControl
    {
        public UcMTQ()
        {
            InitializeComponent();
        }
        connect cn = new connect();
        bool them = true;
        int vitri;
        private void loaddata()
        {
            gridControlMTQ.DataSource = cn.getDataTable("tblMTQ_DS");
        }
        private void batbutton(bool val)
        {
            btnLuu.Enabled = !val;
            txtHoTen.Enabled = !val;
            txtDiaChi.Enabled = !val;
            btnSua.Enabled = val;
            btnThem.Enabled = val;
            btnXoa.Enabled = val;
            gridControlMTQ.Enabled = val;
        }
        private void UcMTQ_Load(object sender, EventArgs e)
        {
            
            batbutton(true);
            loaddata();
        }
        

        private void gridViewMTQ_RowCellStyle(object sender, DevExpress.XtraGrid.Views.Grid.RowCellStyleEventArgs e)
        {
            if (e.RowHandle == gridViewMTQ.FocusedRowHandle)
            {
                e.Appearance.BackColor = Color.DeepSkyBlue;
            }
        }

        private void gridViewMTQ_FocusedRowChanged(object sender, DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs e)
        {
            txtMaMTQ.Text = gridViewMTQ.GetFocusedRowCellDisplayText("MaMTQ");
            txtHoTen.Text = gridViewMTQ.GetFocusedRowCellDisplayText("HoTen");
            txtDiaChi.Text = gridViewMTQ.GetFocusedRowCellDisplayText("DiaChi");
        }

        private void btnThem_Click(object sender, EventArgs e)
        {
            them = true;
            batbutton(false);
            txtHoTen.Text = "";
            txtDiaChi.Text = "";
            txtMaMTQ.Text = "";
            txtHoTen.Focus();
        }

        private void btnSua_Click(object sender, EventArgs e)
        {
            them = false;            
            batbutton(false);
            vitri = int.Parse(gridViewMTQ.FocusedRowHandle.ToString());

        }
        private void btnLuu_Click(object sender, EventArgs e)
        {
            if (txtHoTen.Text == "")
            {
                MessageBox.Show("Họ tên của mạnh tưường quân chưa nhập", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
                txtHoTen.Focus();
            }
            else
            {
                if (them == true)
                {
                    SqlCommand cmd = new SqlCommand("tblMTQ_Them",cn.ketnoi());
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Parameters.Add(new SqlParameter("hoten", SqlDbType.NVarChar)).Value = txtHoTen.Text;
                    cmd.Parameters.Add(new SqlParameter("diachi", SqlDbType.NVarChar)).Value = txtDiaChi.Text;
                    if (cmd.ExecuteNonQuery() == 1)
                    {
                        UcMTQ_Load(sender, e);
                    }
                    else
                    {
                        MessageBox.Show("Thêm mạnh thường quân không thành công","Thông Báo",MessageBoxButtons.OK,MessageBoxIcon.Error);
                    }
                }
                else
                {
                    SqlCommand cmd_sua = new SqlCommand("tblMTQ_Sua",cn.ketnoi());
                    cmd_sua.CommandType = CommandType.StoredProcedure;
                    cmd_sua.Parameters.Add(new SqlParameter("ma",SqlDbType.Int)).Value=txtMaMTQ.Text;
                    cmd_sua.Parameters.Add(new SqlParameter("hoten",SqlDbType.NVarChar)).Value=txtHoTen.Text;
                    cmd_sua.Parameters.Add(new SqlParameter("diachi",SqlDbType.NVarChar)).Value=txtDiaChi.Text;
                    if(cmd_sua.ExecuteNonQuery()==1)
                    {                        
                        UcMTQ_Load(sender,e);
                        gridViewMTQ.FocusedRowHandle = vitri;
                    }
                }
            }
        }

        private void btnXoa_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("Bạn có muốn chắc chắn xóa mạnh thường quân này không, nếu chọn yes mọi dữ liệu liên quan sẽ bị xóa hết", "Thông Báo", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
            {
                SqlCommand cmd = new SqlCommand("tblMTQ_Xoa", cn.ketnoi());
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add(new SqlParameter("ma", SqlDbType.Int)).Value = int.Parse(txtMaMTQ.Text);
                if (cmd.ExecuteNonQuery() == 1)
                {
                    UcMTQ_Load(sender, e);
                    MessageBox.Show("Xóa thông tin thành công","Thông Báo",MessageBoxButtons.OK,MessageBoxIcon.Information);

                }
            }
        }

        private void btnInDS_Click(object sender, EventArgs e)
        {
            frmInDSMTQ frm = new frmInDSMTQ();
            frm.ShowDialog();

        }

        private void txtTim_Enter(object sender, EventArgs e)
        {
            if (txtTim.Text == "Nhập thông tin tìm kiếm")
            {
                txtTim.Text = "";
            }
        }

        private void txtTim_Leave(object sender, EventArgs e)
        {
            if(txtTim.Text=="" || txtTim.Text!="")
            {
                txtTim.Text="Nhập thông tin tìm kiếm";
            }
        }

        private void txtTim_TextChanged(object sender, EventArgs e)
        {
            if (txtTim.Text != "Nhập thông tin tìm kiếm" || txtTim.Text == "")
            {
                SqlCommand cmd = new SqlCommand("tblMTQ_TimKiem", cn.ketnoi());
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add(new SqlParameter("tim", SqlDbType.NVarChar)).Value = txtTim.Text;
                SqlDataAdapter da = new SqlDataAdapter(cmd);
                DataTable dt = new DataTable();
                da.Fill(dt);
                gridControlMTQ.DataSource = dt;
            }
        }

        private void btnReload_Click(object sender, EventArgs e)
        {
            UcMTQ_Load(sender, e);
        }

    }
}
